$(document).ready(function(){
 WxTest.init();
}); 
var WxTest={
	isWX:false,
	userId: window.ID,
	listData:null,
	init:function(){
			this.initFn();
			if(this.userId==null){
				alert("参数有误");
				return;
			}
			//获取网关列表
			var listurl="api/getLockList/"+this.userId;
			this.getData(listurl,1);
			
		},
		initFn:function(){
			if(this.isWeiXin()){
				this.isWX=true;
			}
		},
		getUrlParm:function(name){
			//构造一个含有目标参数的正则表达式对象  
    		var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");  
    		//匹配目标参数  
    		var r = window.location.search.substr(1).match(reg);  
    		//返回参数值  
   			 if (r!=null) return unescape(r[2]);  
    			return null; 
		},
		getData:function(url,type,obj){
			var o=this;
			//var url="http://gateway.pshare.com/api/getLockList/"+o.userId;
				$.ajax({
					type : 'get',
					url :  url ,
					dataType : 'json',
					async : true,
					cache : false,
					success : function(data) {
						if (data.code != 200) {
							if (data.msg)
								alert(data.msg);
							return;
						}
						o.parseDate(data,type,obj);
					},
					error : function(data) {
					}
				});
		},
		parseDate:function(data,type,obj){
			var list=data.data;
			if(type==1){
				this.showPop(list);
			}else if(type==2){
				this.showState(data,obj);
			}else if(type==3){
				this.doUpDown(obj);
			}
			 
		},
		doUpDown:function(obj){
			var upDownUrl="";
			if($(obj).html()=="下降"){
				upDownUrl="api/setLock/"+$(obj).attr("id").split("_")[0]+"/0";
				$(obj).attr("onclick","WxTest.getData("+"'"+upDownUrl+"'"+",3,this)")
				$(obj).html("上升");
			}else{
				upDownUrl="api/setLock/"+$(obj).attr("id").split("_")[0]+"/1";
				$(obj).attr("onclick","WxTest.getData("+"'"+upDownUrl+"'"+",3,this)")
				$(obj).html("下降");
			}
		},
		showState:function(data,obj){
			var data=data.data;
			var str=$(obj).attr("id");
			str=str.split("_")[0];
			var strUpdownState=data.upDownState==1?'下降':'上升';
			var strBuzzerState="";
			
			if(data.buzzerState==0){
				strBuzzerState="关闭";
			}else if(data.buzzerState==1){
				strBuzzerState="开启";
			}
			if(data.upDownState==1){
				$("#"+str+"_updown").html("上升");
			}else{
				$("#"+str+"_updown").html("下降");
			}
			
			var msg="升降状态："+strUpdownState+",电量等级："+data.eleState+",蜂鸣器："+strBuzzerState;
			alert(msg);
		},
		showPop:function(list){
			var html="";
			this.listData=list;
			if(list.length==0){
				$("#contTable tbody").html("无数据");
				return;
			}
			for(var t in list){
				var strState=list[t].upDownState==1?'上升':'下降';
				var actState=list[t].upDownState==1?'0':'1';
				var upDownUrl="api/setLock/"+list[t].mac+"/"+actState;
				var stateUrl="api/getLock/"+list[t].mac;
				html+="<tr>" +
						"<td  width=\"20%\"><button type=\"button\" class=\"btn btn-default disabled btn-sm\" style=\"border:none\">"+list[t].name+"</button></td>" +
						"<td align=\"right\" >" +
						"<button id=\""+list[t].mac+"_updown\" onclick=\"WxTest.getData("+"'"+upDownUrl+"'"+",3,this)\" type=\"button\" class=\"btn btn-info btn-sm\">"+strState+"</button>&nbsp;&nbsp;" +
								"<button id=\""+list[t].mac+"_state\" type=\"button\" onclick=\"WxTest.getData("+"'"+stateUrl+"'"+",2,this)\" class=\"btn btn-info btn-sm\">获取状态</button></td>" +
						"</tr>"
			}
			$("#contTable tbody").html(html);
		}, 
		isWeiXin:function(){
			var ua = navigator.userAgent.toLowerCase();  
		    if(ua.match(/MicroMessenger/i)=="micromessenger") {  
		        return true;  
		    } else {  
		        return false;  
		    }  
		}
}
